
package com.sun.alen.controller;


import com.sun.alen.entity.Exam;
import com.sun.alen.entity.PageBean;
import com.sun.alen.entity.Result;
import com.sun.alen.entity.Student;
import com.sun.alen.service.ExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/exam")
@Slf4j
public class ExamController {

    @Autowired
    private ExamService examService;

    /**
     * 添加成绩
     * @param exam
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Exam exam){
        boolean flag = examService.add(exam);
        if (flag){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改成绩
     * @param exam
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Exam exam){
        boolean flag = examService.update(exam);
        if (flag){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 根据ID删除成绩
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(String id){
        boolean flag=examService.delete(id);
        if (flag){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 搜索成绩
     * @param input
     * @param select
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public Result search(@RequestParam(name = "input", required = false, defaultValue = "") String input,
                         @RequestParam(name = "select", required = false, defaultValue = "1") String select,
                         @RequestParam(name = "page", required = false, defaultValue = "1") Integer currentPage,
                         @RequestParam(name = "size", required = false, defaultValue = "5") Integer pageSize){
        PageBean pageBean = examService.search(input, select, currentPage, pageSize);
        return Result.success(pageBean);
    }


    @GetMapping("/searchTeacher")
    public Result searchTeacher(@RequestParam(name = "input",required = false,defaultValue = "")String input,
                                @RequestParam(name = "select",required = false,defaultValue = "1")String select,
                                @RequestParam(name = "teacherId",required = false,defaultValue = "")Integer teacherId,
                                @RequestParam(name = "page",required = false,defaultValue = "1")Integer currentPage,
                                @RequestParam(name = "size",required = false,defaultValue = "5")Integer pageSize){

        PageBean pageBean=examService.searchTeacher(input,select,teacherId,currentPage,pageSize);
        return Result.success(pageBean);
    }

}
